﻿using GrayParrot.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrayParrot.RabbitMQ.Configuration
{
    public static class RabbitMQSettingsExtension
    {
        /// <summary>
        /// Gets the default endpoint name...
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string GetDefaultEndpointName(this IConfigurationManagerHelper value)
        {
            string retValue = string.Empty;

            RabbitMQSettings cfg = RabbitMQSettings.GetConfiguration(@"GrayParrotGroup/RabbitMQSettings");
            retValue = cfg.Endpoints.Default;

            return retValue;
        }

        /// <summary>
        /// Gets the default endpoint...
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static Endpoint GetDefaultEndpoint(this IConfigurationManagerHelper value)
        {
            Endpoint retValue = null;

            RabbitMQSettings cfg = RabbitMQSettings.GetConfiguration(@"GrayParrotGroup/RabbitMQSettings");
            string m_defaultEndpoint = cfg.Endpoints.Default;

            retValue = GetEndpoint(value, m_defaultEndpoint);

            return retValue;
        }

        /// <summary>
        /// Gets the endpoint.
        /// </summary>
        /// <param name="key">The key name for the endpoint</param>
        /// <returns></returns>
        public static Endpoint GetEndpoint(this IConfigurationManagerHelper value, string key)
        {
            Endpoint retValue = null;

            RabbitMQSettings cfg = RabbitMQSettings.GetConfiguration(@"GrayParrotGroup/RabbitMQSettings");
            retValue = cfg.Endpoints[key];

            return retValue;
        }

        public static Subscriptions GetSubscriptions(this IConfigurationManagerHelper value, string endpoint)
        {
            Subscriptions retValue = null;
            Endpoint m_endpoint = null;

            try
            {
                RabbitMQSettings cfg = RabbitMQSettings.GetConfiguration(@"GrayParrotGroup/RabbitMQSettings");
                m_endpoint = cfg.Endpoints[endpoint];
                retValue = m_endpoint.Subscriptions;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Subscriptions non trovate per l'endpoint '{1}'", endpoint), ex);
            }

            return retValue;
        }

        /// <summary>
        /// Gets the subscription.
        /// </summary>
        /// <param name="endpoint">The endpoint name</param>
        /// <param name="key">The key name for the subscription.</param>
        /// <returns></returns>
        public static Subscription GetSubscription(this IConfigurationManagerHelper value, string endpoint, string key)
        {
            Subscription retValue = null;
            Endpoint m_endpoint = null;

            try
            {
                RabbitMQSettings cfg = RabbitMQSettings.GetConfiguration(@"GrayParrotGroup/RabbitMQSettings");
                m_endpoint = cfg.Endpoints[endpoint];
                retValue = m_endpoint.Subscriptions[key];
                retValue.ConnectionString = m_endpoint.ConnectionString;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Subscription '{0}' non trovata per l'endpoint '{1}'", key, endpoint), ex);
            }

            return retValue;
        }

        /// <summary>
        /// Gets the subscription of the default endpoint.
        /// </summary>
        /// <param name="key">The key name of the subscription.</param>
        /// <returns></returns>
        public static Subscription GetSubscription(this IConfigurationManagerHelper value, string key)
        {
            Subscription retValue = null;
            Endpoint m_endpoint = null;
            string m_defaultEndpoint = string.Empty;

            RabbitMQSettings cfg = RabbitMQSettings.GetConfiguration(@"GrayParrotGroup/RabbitMQSettings");

            try
            {
                m_defaultEndpoint = cfg.Endpoints.Default;
                m_endpoint = cfg.Endpoints[m_defaultEndpoint];
                retValue = m_endpoint.Subscriptions[key];
                retValue.ConnectionString = m_endpoint.ConnectionString;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Subscription '{0}' non trovata per l'endpoint '{1}'", key, m_defaultEndpoint), ex);
            }

            return retValue;
        }
    }
}
